package com.qfedu.service.impl;

import com.qfedu.dao.RotationImageRepository;
import com.qfedu.pojo.entity.RotationImage;
import com.qfedu.pojo.req.RotationImageReq;
import com.qfedu.pojo.resp.BaseResp;
import com.qfedu.service.RotationImageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName RotationImageImpl
 * @Description TODO
 * @Author young
 * @Date 2022-09-27 15:08
 * @Version 1.0
 **/
@Service
public class RotationImageImpl implements RotationImageService {
    @Autowired
    RotationImageRepository rotationImageRepository;
    @Override
    public BaseResp findByType(Integer type) {
        List<RotationImage> byType = rotationImageRepository.findByType(type);
        return new BaseResp(1,"success",byType,null);
    }

    //后台管理的方法
    @Override
    public BaseResp findAll(Integer page, Integer size) {
        //PageRequest.of调用

        PageRequest of = PageRequest.of(page - 1, size);

        Page<RotationImage> all = rotationImageRepository.findAll(of);

        //all.getContent(),结果集
        //all.getTotalElements(),总条数
        return new BaseResp(1,"success",all.getContent(),all.getTotalElements());
    }

    @Override
    public BaseResp save(RotationImageReq rotationImageReq) {
        RotationImage rotationImage = new RotationImage();
        BeanUtils.copyProperties(rotationImageReq,rotationImage);
        System.out.println(rotationImage);
        rotationImageRepository.saveAndFlush(rotationImage);
        //执行成功,返回封装结果
        return new BaseResp().Success();
    }

    @Override
    public BaseResp delById(Integer id) {
        rotationImageRepository.deleteById(id);
        return new BaseResp().Success();
    }
}
